package cn.ekuma.epos.datalogic.define.dao;

import java.util.Date;

import cn.ekuma.data.dao.BaseDAO;
import cn.ekuma.epos.db.table.I_CustomerStockCurrent;

import com.openbravo.bean.CustomerStockCurrent;
import com.openbravo.bean.Location;
import com.openbravo.bean.Product;
import com.openbravo.bean.StockDiary;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;

public class CustomerStorckCurrentDAO extends BaseDAO<CustomerStockCurrent>{

	public CustomerStorckCurrentDAO(I_Session s) {
		super(s);
	}

	@Override
	public CustomerStockCurrent readValues(DataRead dr,CustomerStockCurrent obj) throws BasicException {
		if(obj==null)
			obj=new CustomerStockCurrent();
		obj.setId(dr.getString(1));
		obj.setLocationId(dr.getString(2));
		obj.setProductId(dr.getString(3));
		obj.setAttrsetinstId(dr.getString(4));
		obj.setCustomerId(dr.getString(5));
		obj.setUnits(dr.getDouble(6));
		obj.setValidityDate(dr.getTimestamp(7));
		return obj;
	}

	@Override
	public TableDefinition getTable() {
		return new TableDefinition(s, "CUSTOMER_STOCKCURRENT", new Field[] {
				new Field(I_CustomerStockCurrent.ID, Datas.STRING, Formats.STRING),
				new Field(I_CustomerStockCurrent.LOCATION, Datas.STRING, Formats.STRING,
						Location.class),
				new Field(I_CustomerStockCurrent.PRODUCT, Datas.STRING, Formats.STRING,
						Product.class),
				new Field(I_CustomerStockCurrent.ATTRIBUTESETINSTANCE_ID, Datas.STRING, Formats.STRING),		
				new Field(I_CustomerStockCurrent.CUSTOMER, Datas.STRING,
						Formats.STRING),	
				new Field(I_CustomerStockCurrent.UNITS, Datas.DOUBLE, Formats.DOUBLE),
				new Field(I_CustomerStockCurrent.VALIDITYDATE, Datas.TIMESTAMP, Formats.TIMESTAMP),
				new Field(I_CustomerStockCurrent.CREATEDATE, Datas.TIMESTAMP, Formats.TIMESTAMP)
		},new int[] { 0 });
	}

	@Override
	public void writeInsertValues(DataWrite dp, CustomerStockCurrent obj)
			throws BasicException {
		dp.setString(1,obj.getId());
		dp.setString(2,obj.getLocationId());
		dp.setString(3,obj.getProductId());
		dp.setString(4,obj.getAttrsetinstId());
		dp.setString(5,obj.getCustomerId());
		dp.setDouble(6,obj.getUnits());
		dp.setTimestamp(7,obj.getValidityDate());
		dp.setTimestamp(8,obj.getCreateDate());
	}

	@Override
	public Class getSuportClass() {
		return CustomerStockCurrent.class;
	}

	
	public void stockAdd(final String customer,final StockDiary diary) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		if (diary.getProductAttSetInstId() == null) {
			updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_STOCKCURRENT SET UNITS = (UNITS + ?) WHERE LOCATION = ? AND PRODUCT = ? AND CUSTOMER = ? AND ATTRIBUTESETINSTANCE_ID IS NULL",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setString(2, diary.getLocation());
					setString(3, diary.getProductID());
					setString(4, customer);

				}
			});
		} else
		updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_STOCKCURRENT SET UNITS = (UNITS + ?) WHERE LOCATION = ? AND PRODUCT = ? AND CUSTOMER = ? AND ATTRIBUTESETINSTANCE_ID = ?",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setString(2, diary.getLocation());
					setString(3, diary.getProductID());
					setString(4, customer);
					setString(5,diary.getProductAttSetInstId());
				}
			});
		if (updateresult == 0) {
			CustomerStockCurrent obj=new CustomerStockCurrent();
			obj.setLocationId(diary.getLocation());
			obj.setProductId(diary.getProductID());
			obj.setAttrsetinstId(diary.getProductAttSetInstId());
			obj.setCustomerId(customer);
			obj.setUnits(diary.getMultiply());
			obj.setValidityDate(diary.getValidityDate());
			obj.setCreateDate(new Date());
			insert(obj);
		}
	}

	public void stockSub(final String customer,final StockDiary diary) throws BasicException {
		int updateresult; // si ATTRIBUTESETINSTANCE_ID is null
		if (diary.getProductAttSetInstId() == null) {
			updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_STOCKCURRENT SET UNITS = (UNITS - ?) WHERE LOCATION = ? AND PRODUCT = ? AND CUSTOMER = ? AND ATTRIBUTESETINSTANCE_ID IS NULL",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setString(2, diary.getLocation());
					setString(3, diary.getProductID());
					setString(4, diary.getLotNo());
				}
			});
		} else
			updateresult = new PreparedSentence(
					s,
					"UPDATE CUSTOMER_STOCKCURRENT SET UNITS = (UNITS - ?) WHERE LOCATION = ? AND PRODUCT = ? AND CUSTOMER = ? AND ATTRIBUTESETINSTANCE_ID = ?",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1, diary.getMultiply());
					setString(2, diary.getLocation());
					setString(3, diary.getProductID());
					setString(4, customer);
					setString(5,diary.getProductAttSetInstId());
				}
			});

		if (updateresult == 0) {
			CustomerStockCurrent obj=new CustomerStockCurrent();
			obj.setLocationId(diary.getLocation());
			obj.setProductId(diary.getProductID());
			obj.setAttrsetinstId(diary.getProductAttSetInstId());
			obj.setCustomerId(customer);
			obj.setUnits(diary.getMultiply());
			obj.setValidityDate(diary.getValidityDate());
			obj.setCreateDate(new Date());
			insert(obj);
		}
	}
}